package com.oceansoft.mobile.econsole.modules.situation.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.situation.entity.Situation;
import com.oceansoft.mobile.econsole.modules.situation.persistence.ISituationMapper;
import com.oceansoft.mobile.econsole.modules.situation.service.ISituationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 我的大名叫陆健伟 on 2016/2/17.
 */
@Service
public class SituationServiceImpl extends CacheBaseService implements ISituationService {

    @Resource
    private ISituationMapper situationMapper;

    public Pager<Situation> loadSituationsByPage(Query query){
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Situation> page = (Page<Situation>) situationMapper.loadSituationsByCondition(query);
        return PageUtil.page2pager(page,query);
    }

    @Override
    public Pager<Situation> findSituationByPager(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Situation> page = (Page<Situation>) situationMapper.findSituationByPager(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public List<String> findAllHighwayName() {
        // 从缓存中读取
        List<String> list = (List<String>)super.getObject("app:highway:namelist");
        if(list == null){
            list = situationMapper.queryAllHighwayName();
            // 加入缓存，时间一小时
            super.cache("app:highway:namelist",list,3600);
        }

        return list;
    }

    public Situation loadSituationById(String id){
        Situation situation = situationMapper.findById(id);
        return situation;
    }

    public Result savaSituation(Situation situation){
        int id = situationMapper.save(situation);
        if(id==1){
            return new Result(Result.SUCC,"保存成功");
        }
        return new Result(Result.FAILURE,"保存失败");
    }

    public Result updateSituation(Situation situation){
        int id = situationMapper.update(situation);
        if(id==1){
            return new Result(Result.SUCC,"更新成功");
        }
        return new Result(Result.FAILURE,"更新失败");
    }


    public Result deleteSituation(String situationId){
        int id = situationMapper.delete(situationId);
        if(id==1){
            return new Result(Result.SUCC,"删除成功");
        }
        return new Result(Result.FAILURE,"删除失败");
    }

}
